package dragon.social.core;

import dragon.social.entity.Userinfo;
import dragon.social.mapper.UserinfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionKey;
import org.springframework.social.connect.ConnectionSignUp;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.stereotype.Component;

/**
 * 后台自动注册的实现类。理论上此类应该写在业务逻辑模组上
 */
@Component
public class AutoConnectionSignUp implements ConnectionSignUp {

    @Autowired
    UserinfoMapper userinfoMapper;

    /**
     * Sign up a new user of the application from the connection.
     *
     * @param connection the connection
     * @return the new user id. May be null to indicate that an implicit local user profile could not be created.
     */
    @Override
    public String execute(Connection<?> connection) {
        ConnectionKey key = connection.getKey();
        String providerId = key.getProviderId();
        Userinfo userinfo = new Userinfo();
        userinfo.setDisplayName(connection.getDisplayName());
        userinfo.setLoginId(connection.getDisplayName());
        //这里不能用passwordEncoder，会报循环注入错误
        userinfo.setPassword("12345");
        userinfoMapper.insert(userinfo);
        Long userId = userinfo.getUserId();
        //注意看方法解释：the new user id
        return userId + "";
    }
}
